import { onLoad } from "@dcloudio/uni-app";
import { nilV } from "utils";
import { useAppStore } from "@/store";
import { ref } from "vue";
import { AppNotificationEnum } from "@/event/globalNotification/eventSource/const";
import { emitNotification } from "@/event";
import type { UniLifeCycleOpt } from "../lifeCycle.type";

function uniOnLoad(o?: UniLifeCycleOpt) {
    const hasLoadRef = ref(false);
    let { needLogin, cb } = o || {};
    onLoad(function (opt) {
        const appStore = useAppStore();
        let options = opt || {};
        hasLoadRef.value = true;
        // 判断是否 需要登录
        if (needLogin !== false) {
            if (nilV(appStore.appUserRef)) {
                emitNotification(AppNotificationEnum.logout);
                return;
            }
        }
        // 回调
        cb && cb(options);
    });
    return hasLoadRef;
}

export { uniOnLoad };
